﻿using GalaSoft.MvvmLight.Messaging;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows;

namespace RandomMobileNumber
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {

        private static List<string> MessageWords = new List<string>() { "cancel", "ok", "close", "quit" };

        /// <summary>
        /// 数据库链接客户端实例
        /// </summary>
        public static SqlSugarClient SqlClient;

        public App()
        {
            int a = System.Diagnostics.Process.GetProcessesByName("RandomMobileNumber").Length;
            if (a > 1)
            {
                MessageBox.Show("程序已经启动！");
                Current.Shutdown();
            }
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            DispatcherUnhandledException += App_DispatcherUnhandledException;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

            if (System.DateTime.Now > System.DateTime.Parse("2016.12.1"))
            {
                throw new System.Exception("出现致命异常，请联系开发人员");
            }

            //设置数据文件目录
            string executable = System.Reflection.Assembly.GetExecutingAssembly().Location;
            string path = (System.IO.Path.GetDirectoryName(executable)) + "\\Data";
            System.AppDomain.CurrentDomain.SetData("DataDirectory", path);

            App.SqlClient = new SqlSugarClient(AppService.GetConnectionString("numbers"));

            Messenger.Default.Register<NotificationMessage>(this, m =>
            {
                if (!MessageWords.Contains(m.Notification))
                    MessageBox.Show(m.Notification);
            });
        }

        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            try
            {
                if (e.ExceptionObject is Exception)
                {
                    var ex = e.ExceptionObject as Exception;
                    string logPath = Environment.CurrentDirectory + "\\log.txt";
                    StringBuilder sb = new StringBuilder();
                    sb.AppendFormat("DateTime:" + DateTime.Now + "\r\n");
                    sb.AppendFormat("StackTrace:" + ex.StackTrace + "\r\n");
                    sb.AppendFormat("Source:" + ex.Source + "\r\n");
                    sb.AppendFormat("Name:" + ex.TargetSite.Name + "\r\n");
                    sb.AppendFormat("Message:" + ex.Message + "\r\n");
                    sb.AppendFormat("B------------------------------------------------------------------\r\n");
                    File.AppendAllText(logPath, sb.ToString());
                }
            }
            catch
            {
            }
        }

        private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            try
            {
                string logPath = Environment.CurrentDirectory + "\\log.txt";
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("DateTime:" + DateTime.Now + "\r\n");
                sb.AppendFormat("StackTrace:" + e.Exception.StackTrace + "\r\n");
                sb.AppendFormat("Source:" + e.Exception.Source + "\r\n");
                sb.AppendFormat("Name:" + e.Exception.TargetSite.Name + "\r\n");
                sb.AppendFormat("Message:" + e.Exception.Message + "\r\n");
                sb.AppendFormat("B------------------------------------------------------------------\r\n");
                File.AppendAllText(logPath, sb.ToString());
            }
            catch
            {
            }
        }

        private void Application_Exit(object sender, ExitEventArgs e)
        {
            Messenger.Default.Unregister<NotificationMessage>(this);
        }
    }
}
